package com.face.zf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

public class Base64Utils {
    public static byte[] decodeBase64(String base64Str) {
        // 移除可能存在的Base64头部描述（如"data:image/png;base64,"）
        String cleanBase64 = base64Str;
        if (base64Str.startsWith("data:")) {
            cleanBase64 = base64Str.replaceFirst("data:[^;]+;base64,", "");
        }
        return Base64.getDecoder().decode(cleanBase64);
    }

    public static String convertImageToBase64(String filePath) throws IOException {
        File file = new File(filePath);
        byte[] fileContent = new byte[(int) file.length()];

        try (FileInputStream inputStream = new FileInputStream(file)) {
            inputStream.read(fileContent);
        }

        return Base64.getEncoder().encodeToString(fileContent);
    }
}
